package com.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.common.utils.PageUtils;
import com.java.common.utils.Query;
import com.java.mapper.StuDao;
import com.java.pojo.Stu;
import com.java.service.StuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service("stuService")
public class StuServiceImpl extends ServiceImpl<StuDao, Stu> implements StuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Stu> page = this.page(
                new Query<Stu>().getPage(params),
                // 拼接查询条件
                new LambdaQueryWrapper<Stu>()
        );

        return new PageUtils(page);
    }

    public void saveParent() {
        Stu stu = new Stu();
        stu.setName("parent");
        stu.setAge(19);
        this.baseMapper.insert(stu);
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveChildren() {
        saveChild1();
        int a = 1 / 0;
        saveChild2();
    }

    public void saveChild1() {
        Stu stu1 = new Stu();
        stu1.setName("child-1");
        stu1.setAge(11);
        this.baseMapper.insert(stu1);
    }
    public void saveChild2() {
        Stu stu2 = new Stu();
        stu2.setName("child-2");
        stu2.setAge(22);
        this.baseMapper.insert(stu2);
    }

}